package com.parse.tree.parsers.service.java;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

	/**
	 * List all files from a directory and its subdirectories
	 * 
	 * @param directoryName
	 *            to be listed
	 */
	public static List<String> listFilesAndFilesSubDirectories(
			List<String> filesList,String directoryName) {
		try {
			File directory = new File(directoryName);
			if(!directory.exists())
				return null;
			// get all the files from a directory
			File[] fList = directory.listFiles();

			for (File file : fList) {
				if (file.isFile()) {
					String fileName = file.getName();
					if (fileName.contains(".java")) {
						filesList.add(file.getAbsolutePath());
					}
				} else if (file.isDirectory()) {
					listFilesAndFilesSubDirectories(filesList,file.getAbsolutePath());
				}
			}
		} catch (Exception e) {
			System.out.println("*****Error in retrieving files:" + e);
			return null;
		}
		return filesList;
	}

	public static void main(String args[]) {
		List<String> filesList = new ArrayList<String>();
		FileUtil.listFilesAndFilesSubDirectories(filesList,"/Users/sam/Documents/workspace/AntlrJavaParser/src/main/java");
		System.out.println(filesList);
	}

}
